<script setup>
import { onMounted, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { getOrderAPI } from '@/apis/pay'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'

const route = useRoute()
const router = useRouter()
const userStore = useUserStore()
const order = ref({})

onMounted(async () => {
  const orderId = route.query.id
  const userId = userStore.userInfo?.id
  
  if (!orderId || !userId) {
    ElMessage.error('订单信息不完整')
    setTimeout(() => {
      router.push('/')
    }, 2000)
    return
  }
  
  try {
    const res = await getOrderAPI(orderId, userId)
    order.value = res.result || {}
    
    if (!order.value.id) {
      ElMessage.error('订单不存在')
      setTimeout(() => {
        router.push('/')
      }, 2000)
    }
  } catch (error) {
    console.error('获取订单信息失败', error)
    ElMessage.error('获取订单信息失败')
  }
})

// 再次购买
const handleBuyAgain = () => {
  router.push('/')
}

// 查看订单
const handleViewOrder = () => {
  router.push('/member/order')
}
</script>

<template>
  <div class="xtx-pay-page">
    <div class="container">
      <!-- 支付结果 -->
      <div class="pay-result">
        <span class="iconfont icon-queren2 green"></span>
        <div class="tip">
          <p>支付成功</p>
          <p>感谢您在本站购物，欢迎再次光临！</p>
        </div>
        <div class="actions">
          <el-button type="primary" size="large" @click="handleBuyAgain">继续逛</el-button>
          <el-button size="large" @click="handleViewOrder">查看订单</el-button>
        </div>
        <p class="text">
          支付金额：<span>¥{{ order.payMoney?.toFixed(2) }}</span>
        </p>
        <p class="text">
          订单编号：<span>{{ order.id }}</span>
        </p>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.xtx-pay-page {
  margin-top: 20px;
}

.pay-result {
  padding: 100px 0;
  background: #fff;
  text-align: center;
  
  > .iconfont {
    font-size: 100px;
  }
  
  .green {
    color: #1dc779;
  }
  
  .tip {
    margin-top: 20px;
    
    p {
      line-height: 26px;
      font-size: 16px;
      color: #999;
      
      &:first-child {
        font-size: 22px;
        color: #666;
      }
    }
  }
  
  .actions {
    margin-top: 50px;
    
    .btn {
      margin: 0 10px;
    }
  }
  
  .text {
    margin-top: 20px;
    font-size: 16px;
    color: #999;
    
    span {
      color: var(--price-color);
      font-weight: bold;
    }
  }
}
</style> 